package com.itany.shop.service.proxy;

import com.itany.shop.entity.Order;
import com.itany.shop.entity.User;
import com.itany.shop.exception.RequestParameterErrorException;
import com.itany.shop.exception.UserNotLoginException;
import com.itany.shop.factory.ObjectFactory;
import com.itany.shop.service.OrderService;
import com.itany.shop.tran.TransactionManager;
import com.itany.shop.vo.Cart;

import java.util.List;

/**
 * @author Miss Chen
 * @version 1.0
 * @date 2024年08月16日14:46
 */
public class OrderServiceProxy implements OrderService {
    private TransactionManager tran= (TransactionManager) ObjectFactory.getObject("tran");
    private OrderService orderService= (OrderService) ObjectFactory.getObject("orderServiceTarget");
    @Override
    public String createOrder(Cart cart, User user) throws UserNotLoginException {
        try {
            tran.begin();
            String no=orderService.createOrder(cart,user);
            tran.commit();
            return no;
        } catch (UserNotLoginException e) {
            tran.rollback();
            throw e;
        }
    }

    @Override
    public List<Order> findMyOrders(User user) throws UserNotLoginException {
        try {
            tran.begin();
            List<Order> orders=orderService.findMyOrders(user);
            tran.commit();
            return orders;
        } catch (UserNotLoginException e) {
            tran.rollback();
            throw e;
        }

    }

    @Override
    public Order findOrderDetail(Integer id) throws RequestParameterErrorException {
        try {
            tran.begin();
            Order order=orderService.findOrderDetail(id);
            tran.commit();
            return order;
        } catch (RequestParameterErrorException e) {
            tran.rollback();
            throw e;
        }
    }
}
